/**
 * Carnegie Mellon University
 * School of Computer Science
 * MSIT eBusiness Technology
 * 
 * Team 8 - Infinity (2011 Spring)
 * infinity-team8@googlegroups.com
 * 
 * Professor: Jeffrey L. Eppinger
 * Program Faculty: Sujata Telang
 */

package databean;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.sql.Date;
import java.util.Random;

public class CustomerBean implements Comparable<CustomerBean> {
	private String  userName 	   = null;
	private String  firstName      = null;
	private String  lastName       = null;
	private String  hashedPassword = "*";
	private int     salt           = 0;
	
	private long	customerID	   = 0;
	private String	addressLine1   = null;
	private String	addressLine2   = null;
	private String	city		   = null;
	private String	state		   = null;
	private String	zip			   = null;
	private long	cash		   = 0;
	private Date	lastTradingDay = null;
	

	
	
	public CustomerBean(long customerID) {
		this.customerID = customerID;
	}

	public boolean checkPassword(String password) {
		return hashedPassword.equals(hash(password));
	}
	
	public int compareTo(CustomerBean other) {
		// Order first by lastName and then by firstName
		int c = lastName.compareTo(other.lastName);
		if (c != 0) return c;
		c = firstName.compareTo(other.firstName);
		if (c != 0) return c;
		return userName.compareTo(other.userName);
	}

	public boolean equals(Object obj) {
		if (obj instanceof CustomerBean) {
			CustomerBean other = (CustomerBean) obj;
			return userName.equals(other.userName);
		}
		return false;
	}

	public String  getHashedPassword() { return hashedPassword; }
	public String  getUserName()       { return userName;       }
	public int     getSalt()           { return salt;           }

	public String  getFirstName()      { return firstName;      }
	public String  getLastName()       { return lastName;       }

	public int     hashCode()          { return userName.hashCode(); }

	public void setHashedPassword(String x) { hashedPassword = x; }
	public void setPassword(String s)       { salt = newSalt(); hashedPassword = hash(s); }
	public void setSalt(int x)              { salt = x;           }

	public void setFirstName(String s)      { firstName = s;      }
	public void setLastName(String s)       { lastName = s;       }

	public String toString() {
		return "User("+getUserName()+")";
	}

	private String hash(String clearPassword) {
		if (salt == 0) return null;

		MessageDigest md = null;
		try {
		  md = MessageDigest.getInstance("SHA1");
		} catch (NoSuchAlgorithmException e) {
		  throw new AssertionError("Can't find the SHA1 algorithm in the java.security package");
		}

		String saltString = String.valueOf(salt);
		
		md.update(saltString.getBytes());
		md.update(clearPassword.getBytes());
		byte[] digestBytes = md.digest();

		// Format the digest as a String
		StringBuffer digestSB = new StringBuffer();
		for (int i=0; i<digestBytes.length; i++) {
		  int lowNibble = digestBytes[i] & 0x0f;
		  int highNibble = (digestBytes[i]>>4) & 0x0f;
		  digestSB.append(Integer.toHexString(highNibble));
		  digestSB.append(Integer.toHexString(lowNibble));
		}
		String digestStr = digestSB.toString();

		return digestStr;
	}

	private int newSalt() {
		Random random = new Random();
		return random.nextInt(8192)+1;  // salt cannot be zero
	}

	public String getAddressLine1() {
		return addressLine1;
	}

	public void setAddressLine1(String addressLine1) {
		this.addressLine1 = addressLine1;
	}

	public String getAddressLine2() {
		return addressLine2;
	}

	public void setAddressLine2(String addressLine2) {
		this.addressLine2 = addressLine2;
	}

	public String getCity() {
		return city;
	}

	public void setCity(String city) {
		this.city = city;
	}

	public String getState() {
		return state;
	}

	public void setState(String state) {
		this.state = state;
	}

	public String getZip() {
		return zip;
	}

	public void setZip(String zip) {
		this.zip = zip;
	}

	

	public long getCash() {		
		return cash;
	}

	public void setCash(long cash) {
		this.cash = cash;
	}

	public long getCustomerID() {
		return customerID;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public Date getLastTradingDay() {
		return lastTradingDay;
	}

	public void setLastTradingDay(Date lastTradingDay) {
		this.lastTradingDay = lastTradingDay;
	}
	
	
}
